# fpath="D:\pydataset\MSRA\msra_dev.bio"
# outfpath="D:\pydataset\MSRA\msra_dev.bmes"
# fpath="D:\pydataset\MSRA\msra_test.bio"
# outfpath="D:\pydataset\MSRA\msra_test.bmes"
# fpath="D:\pydataset\MSRA\msra_train.bio"
# outfpath="D:\pydataset\MSRA\msra_train.bmes"

# fpath="D:\pydataset\PeopleDaily\example.dev.bio"
# outfpath="D:\pydataset\PeopleDaily\example.dev.bmes"
# fpath="D:\pydataset\PeopleDaily\example.test.bio"
# outfpath="D:\pydataset\PeopleDaily\example.test.bmes"
# fpath="D:\pydataset\PeopleDaily\example.train.bio"
# outfpath="D:\pydataset\PeopleDaily\example.train.bmes"

# ResumeNER本来就是bmes

# fpath="D:\pydataset\Weibo\weiboNER_2nd_conll.dev.bio"
# outfpath="D:\pydataset\Weibo\weiboNER.dev.bmes"
# fpath="D:\pydataset\Weibo\weiboNER_2nd_conll.test.bio"
# outfpath="D:\pydataset\Weibo\weiboNER.test.bmes"
fpath="D:\pydataset\Weibo\weiboNER_2nd_conll.train.bio"
outfpath="D:\pydataset\Weibo\weiboNER.train.bmes"

with open(fpath,"r",encoding='utf-8') as r:
    with open(outfpath,"w",encoding='utf-8') as w:
        pre = r.readline().split()
        nowline=r.readline()##数据集应该不至于少于两行吧
        while nowline:
            line = nowline.split()
            print(line)
            if line==[]:
                if pre[1][0] == 'O':
                    w.write(pre[0][0]+' ')#写入前部分的第一个字符
                    w.write(pre[1])    #还是O,直接写入
                    w.write('\n')
                elif pre[1][0] == 'I':
                    w.write(pre[0][0]+' ')
                    w.write('E')
                    w.write(pre[1][1:])#写入后部分的类别
                    w.write('\n')
                else:
                    w.write(pre[0][0]+' ')
                    w.write('S')
                    w.write(pre[1][1:])
                    w.write('\n')
                w.write('\n')
                pre = r.readline().split() #重新读取两行，用continue跳过结尾的读取
                nowline = r.readline()
                continue
            # MSRA数据集有单行一个0，不清楚原因，已删除
            if line[1][0]=='O':## O最多，早判断有利于节省时间
                if pre[1][0] == 'O':
                    w.write(pre[0][0]+' ')#写入前部分的第一个字符
                    w.write(pre[1])     #还是O,直接写入
                    w.write('\n')
                elif pre[1][0] == 'I':
                    w.write(pre[0][0]+' ')
                    w.write('E')
                    w.write(pre[1][1:])#写入后部分的类别
                    w.write('\n')
                else:
                    w.write(pre[0][0]+' ')
                    w.write('S')
                    w.write(pre[1][1:])
                    w.write('\n')
            elif line[1][0]=='I':
                if pre[1][0] == 'I':
                    w.write(pre[0][0]+' ')
                    w.write('M')
                    w.write(pre[1][1:])
                    w.write('\n')
                else:## B就原样输入
                    w.write(pre[0][0]+' ')
                    w.write(pre[1])
                    w.write('\n')
            else:
                if pre[1][0] == 'O':
                    w.write(pre[0][0]+' ')
                    w.write(pre[1])
                    w.write('\n')
                elif pre[1][0] == 'I':
                    w.write(pre[0][0]+' ')
                    w.write('E')
                    w.write(pre[1][1:])
                    w.write('\n')
                else:
                    w.write(pre[0][0]+' ')
                    w.write('S')
                    w.write(pre[1][1:])
                    w.write('\n')
            pre=line
            nowline=r.readline()
print('运行结束，输出文件保存在'+outfpath)
